package cn.jdy.task.service.classify;


import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;

import cn.jdy.authority.entity.Organ;
import cn.jdy.authority.service.OrganService;
import cn.jdy.common.util.XMLUtil;
import cn.jdy.framework.log.BaseLog;
import cn.jdy.framework.util.DateUtil;
import cn.jdy.framework.util.StringUtil;
import cn.jdy.plugins.entity.ApplyCash;
import cn.jdy.plugins.entity.Maid;
import cn.jdy.plugins.entity.Member;
import cn.jdy.plugins.service.ApplyCashService;
import cn.jdy.plugins.service.CRateOrderService;
import cn.jdy.plugins.service.LiveService;
import cn.jdy.plugins.service.MaidService;
import cn.jdy.plugins.service.MemberService;
import cn.jdy.plugins.service.PointRecordService;
import cn.jdy.plugins.service.ProductxService;
import cn.jdy.plugins.service.WatchUserService;
import cn.jdy.plugins.service.WebsiteService;
import cn.jdy.plugins.wap.util.ChatUtil;
import cn.jdy.plugins.wap.util.HttpUtils;
import cn.jdy.plugins.wap.util.RedPacketUtil;
import cn.jdy.task.service.TaskGroup;

import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.context.ContextLoader;
import org.springframework.web.context.WebApplicationContext;




@TaskGroup("提现情况")
public class Websitedingmo implements Job {
	@Autowired
	private WebsiteService websiteService;
	@Autowired
	private CRateOrderService cRateOrderService;
	@Autowired
	private ProductxService productxService;
	@Autowired
	private MemberService memberService;
	@Autowired
	private PointRecordService pointRecordService;
	@Autowired
	private ApplyCashService applyCashService;
	@Autowired
	private WatchUserService watchUserService;
	@Autowired
	private MaidService maidService;
	@Autowired
	private OrganService organService;
	@Autowired
	private LiveService liveService;

	//企业付款接口地址
	private static final String TRANSFERS_PAY = "https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers"; 
	
	@Override
	public void execute(JobExecutionContext context) throws JobExecutionException {
		try {
			System.out.println("执行了一次");
		
			List<Member> list5 =memberService.find("userLevel","5");//1星
			List<Member> list6 =memberService.find("userLevel","6");//2星
			List<Member> list7 =memberService.find("userLevel","7");//3星
			List<Member> list8 =memberService.find("userLevel","8");//4星
			List<Member> list9 =memberService.find("userLevel","9");//5星
			double	money =  memberService.findcheckDecimal();
		
			if(list9.size()>0){  //5
				double de=0;
				de=money*0.05/(list5.size()+list6.size()+list7.size()+list8.size()+list9.size()) + 
	    		money*0.04/(list6.size()+list7.size()+list8.size()+list9.size()) + 
	    		money*0.03/(list7.size()+list8.size()+list9.size()) +
	    		money*0.02/(list8.size()+list9.size()) +
	    		money*0.02/(list8.size()) ;
				for (Member member : list9) {  
					member.setMoney(member.getMoney()+de);
					member.setKtxmoney(member.getKtxmoney()+de);
					Maid  ma3=new Maid();
					ma3.setOrderNo("0");
		  		  	ma3.setUsername(member.getUserName());
		  		  	ma3.setMch_id("0");
		  		  	ma3.setMemberid(member.getId());
		  			ma3.setProductxid("0");
		  			ma3.setProductxname("5星运营分期");
		  			ma3.setCreateTime(DateUtil.getCurrentDate(DateUtil._DEFAULT2));
		  			ma3.setTotal(0);
  		  	  		ma3.setWermemberid(member.getId());
		  		  	ma3.setWerusername(member.getUserName());
		  		  	ma3.setIsfa("0");
		  		  	ma3.setFendtotal(de);
  		  	  	  	maidService.save(ma3);
  		  	  	  	memberService.update(member);
  		  	  	  	
		  		}
			}
				if(list9.size()>0){  //4
					double	 de=0;
					de=money*0.05/(list5.size()+list6.size()+list7.size()+list8.size()+list9.size()) + 
		    		money*0.04/(list6.size()+list7.size()+list8.size()+list9.size()) + 
		    		money*0.03/(list7.size()+list8.size()+list9.size()) +
		    		money*0.02/(list8.size()+list9.size());
	
					for (Member member : list8) {  
						member.setMoney(member.getMoney()+de);
						member.setKtxmoney(member.getKtxmoney()+de);
						Maid  ma3=new Maid();
						ma3.setOrderNo("0");
			  		  	ma3.setUsername(member.getUserName());
			  		  	ma3.setMch_id("0");
			  		  	ma3.setMemberid(member.getId());
			  			ma3.setProductxid("0");
			  			ma3.setProductxname("4星运营分期");
			  			ma3.setCreateTime(DateUtil.getCurrentDate(DateUtil._DEFAULT2));
			  			ma3.setTotal(0);
	  		  	  		ma3.setWermemberid(member.getId());
			  		  	ma3.setWerusername(member.getUserName());
			  		  	ma3.setIsfa("0");
			  		  	ma3.setFendtotal(de);
	  		  	  	  	maidService.save(ma3);
	  		  	  	  	memberService.update(member);
	  		  	  	  	
			  		}
				}	
					
					if(list9.size()>0){  //3
						double	 de=0;
						de=money*0.05/(list5.size()+list6.size()+list7.size()+list8.size()+list9.size()) + 
			    		money*0.04/(list6.size()+list7.size()+list8.size()+list9.size()) + 
			    		money*0.03/(list7.size()+list8.size()+list9.size());
			    
						for (Member member : list7) {  
							member.setMoney(member.getMoney()+de);
							member.setKtxmoney(member.getKtxmoney()+de);
							Maid  ma3=new Maid();
							ma3.setOrderNo("0");
				  		  	ma3.setUsername(member.getUserName());
				  		  	ma3.setMch_id("0");
				  		  	ma3.setMemberid(member.getId());
				  			ma3.setProductxid("0");
				  			ma3.setProductxname("3星运营分期");
				  			ma3.setCreateTime(DateUtil.getCurrentDate(DateUtil._DEFAULT2));
				  			ma3.setTotal(0);
		  		  	  		ma3.setWermemberid(member.getId());
				  		  	ma3.setWerusername(member.getUserName());
				  		  	ma3.setIsfa("0");
				  		  	ma3.setFendtotal(de);
		  		  	  	  	maidService.save(ma3);
		  		  	  	  	memberService.update(member);
		  		  	  	  	
				  		}
					}	
						
						if(list9.size()>0){  //2
							double	 de=0;
							de=money*0.05/(list5.size()+list6.size()+list7.size()+list8.size()+list9.size()) + 
				    		money*0.04/(list6.size()+list7.size()+list8.size()+list9.size());
				    
							for (Member member : list6) {  
								member.setMoney(member.getMoney()+de);
								member.setKtxmoney(member.getKtxmoney()+de);
								Maid  ma3=new Maid();
								ma3.setOrderNo("0");
					  		  	ma3.setUsername(member.getUserName());
					  		  	ma3.setMch_id("0");
					  		  	ma3.setMemberid(member.getId());
					  			ma3.setProductxid("0");
					  			ma3.setProductxname("2星运营分期");
					  			ma3.setCreateTime(DateUtil.getCurrentDate(DateUtil._DEFAULT2));
					  			ma3.setTotal(0);
			  		  	  		ma3.setWermemberid(member.getId());
					  		  	ma3.setWerusername(member.getUserName());
					  		  	ma3.setIsfa("0");
					  		  	ma3.setFendtotal(de);
			  		  	  	  	maidService.save(ma3);
			  		  	  	  	memberService.update(member);
			  		  	  	  	
					  		}
			}	
						
						if(list9.size()>0){  //1
							double	 de=0;
							de=money*0.05/(list5.size()+list6.size()+list7.size()+list8.size()+list9.size());
							for (Member member : list5) {  
								member.setMoney(member.getMoney()+de);
								member.setKtxmoney(member.getKtxmoney()+de);
								Maid  ma3=new Maid();
								ma3.setOrderNo("0");
					  		  	ma3.setUsername(member.getUserName());
					  		  	ma3.setMch_id("0");
					  		  	ma3.setMemberid(member.getId());
					  			ma3.setProductxid("0");
					  			ma3.setProductxname("1星运营分期");
					  			ma3.setCreateTime(DateUtil.getCurrentDate(DateUtil._DEFAULT2));
					  			ma3.setTotal(0);
			  		  	  		ma3.setWermemberid(member.getId());
					  		  	ma3.setWerusername(member.getUserName());
					  		  	ma3.setIsfa("0");
					  		  	ma3.setFendtotal(de);
			  		  	  	  	maidService.save(ma3);
			  		  	  	  	memberService.update(member);
			  		  	  	  	
					  		}
			}	
		
			//查询所有用户
			
		} catch(Exception e) {
			BaseLog.e(this.getClass(), "invoke", e);
		}
	}
	
	
	
public static void main(String[] args) throws ParseException {
 
       
	   SimpleDateFormat df = new SimpleDateFormat("yyyy-MM");//设置日期格式
       System.out.println(df.format(new Date()));// new Date()为获取当前系统时间
       
	    
	     SimpleDateFormat s=new SimpleDateFormat("yyyy-MM-dd");
	     Calendar calendar = Calendar.getInstance();
	     Date dqs=   s.parse("2018-10-13"); //到期时间
	        calendar.setTime(dqs);
	        calendar.add(Calendar.DATE, -1);
	        s.format(calendar.getTime());

	     Date dq= new Date(); //当前时间
	     
	     System.out.println(dq.getTime()<dqs.getTime());
}
}
